package com.dingreading.cloud.soms.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.dto.AliyunBookDto;
import com.dingreading.cloud.common.dto.BookSimpleDto;
import com.dingreading.cloud.common.dto.LoginDataDto;
import com.dingreading.cloud.common.util.AccountUtil;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.aliyun.AliyunBookUtil;
import com.dingreading.cloud.soms.dto.StoreBooksDto;
import com.dingreading.cloud.soms.entity.Books;
import com.dingreading.cloud.soms.entity.StoreBooks;
import com.dingreading.cloud.soms.service.BooksService;
import com.dingreading.cloud.soms.service.StoreBooksService;
import com.dingreading.cloud.soms.util.BooksUtil;
import com.dingreading.cloud.soms.util.LoginInfoUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/front/storeBooks")
@Api(tags = "门店-门店书籍管理相关")
public class FrontStoreBooksController {

    @Resource
    private StoreBooksService storeBooksService;
    @Resource
    private BooksService booksService;

    @ApiOperation(value = "书籍分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "书刊id") @RequestParam(value = "journalId", required = false) Long journalId,
            @ApiParam(value = "分类id") @RequestParam(value = "bookLabelId", required = false) Long bookLabelId,
            @ApiParam(value = "状态：-1.下架 1.上架") @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "isbn查询状态：1.查isbn为空 2.查询isbn重复") @RequestParam(value = "isbnStatus", required = false) Integer isbnStatus,
            @ApiParam(value = "书号/isbn/书名/作者") @RequestParam(value = "keyword", required = false) String keyword
    ) {
        // 获取员工信息
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();
        long storeId = loginDto.getCurrentStoreId();

        Page<StoreBooksDto> page = storeBooksService.pageList(pageUtil, storeId, keyword, status, journalId, bookLabelId, isbnStatus);

        PageDto<StoreBooksDto> data = new PageDto<>();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }

    @ApiOperation(value = "获取书籍详情")
    @PostMapping(value = "/getById", produces = "application/json; charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public R<Object> getById(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "门店书籍id") @RequestParam(value = "id") Long id
    ) throws Exception {
        StoreBooks storeBooks = storeBooksService.getById(id);
        if (storeBooks == null) return R.fail("书籍获取失败");

        return R.ok(storeBooks);
    }

    @ApiOperation(value = "删除书籍")
    @PostMapping(value = "/deleteBook", produces = "application/json; charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public R<Object> deleteBook(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "门店书籍id") @RequestParam(value = "id") Long id
    ) throws Exception {
        StoreBooks storeBooks = storeBooksService.getById(id);
        if (storeBooks == null) return R.fail("书籍获取失败");

        // 非系统录入的书籍，可删除
        if (storeBooks.getSource() != null && !storeBooks.getSource().equals(1)) {
            storeBooksService.removeById(id);
//            if (storeBooks.getSource().equals(2)) {
//                Books books = booksService.getById(storeBooks.getBooksId());
//                // 本门店用阿里云录入的书籍，可删除
//                if (books != null && books.getSource() != null && books.getSource().equals(2)) {
//                    if (books.getSourceStoreId() != null && books.getSourceStoreId().equals(storeId)) {
//                        booksService.delete(storeBooks.getBooksId());
//                    }
//                }
//            }
        } else {
            return R.fail("系统录入的书籍，不能删除");
        }
        return R.ok(storeBooks);
    }

    @ApiOperation(value = "更新书籍的状态")
    @PostMapping(value = "/updateStatus", produces = "application/json; charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public R<Object> updateStatus(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "门店书籍id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "状态：-1.下架 1.上架") @RequestParam(value = "status") Integer status
    ) throws Exception {
        storeBooksService.updateStatus(id, status);
        return R.ok();
    }

    @ApiOperation(value = "判断是否能添加书籍")
    @PostMapping(value = "/whetherAddBook", produces = "application/json; charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public R<Object> whetherAddBook(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "isbn/书号") @RequestParam(value = "isbn") String isbn
    ) {
        // 获取员工信息
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();
        long storeId = loginDto.getCurrentStoreId();

        if (isbn.length() != 13 && isbn.length() != 8) return R.fail("isbn号或书号异常");

        List<BookSimpleDto> list = new ArrayList<>();

        // 判断门店书籍，相同的isbn和书号
        List<Long> bookIds = null;
        List<StoreBooks> storeBookAndList = storeBooksService.getByStoreIdAndIsbnOrBookNum(storeId, isbn);
        if (CollectionUtils.isNotEmpty(storeBookAndList)) {
            List<BookSimpleDto> bookSimpleList = BooksUtil.getBookSimpleDtoByStoreBook(storeBookAndList);
//            return R.fail(4001, "发现相同的门店书籍", bookSimpleList);
            list.addAll(bookSimpleList);

            bookIds = bookSimpleList.stream().filter(bookSimpleDto -> bookSimpleDto.getBooksId() != null)
                    .map(BookSimpleDto::getBooksId).collect(Collectors.toList());
        }

        // 判断系统书籍，相同的isbn和书号
        List<Books> booksList = booksService.getByIsbnOrBookNumNotInId(isbn, bookIds);
        if (CollectionUtils.isNotEmpty(booksList)) {
            List<BookSimpleDto> bookSimpleList = BooksUtil.getBookSimpleDto(booksList);
//            return R.fail(4002, "发现相同的系统书籍，是否导入", bookSimpleList);
            list.addAll(bookSimpleList);
        }

        if (CollectionUtils.isNotEmpty(list)) {
            return R.fail(4001, "发现相同isbn的书籍", list);
        }

        return R.ok();
    }

    @ApiOperation(value = "将书籍添加到门店（系统书籍优先添加）")
    @PostMapping(value = "/bookAddToStore", produces = "application/json; charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public R<Object> bookAddToStore(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "书籍id") @RequestParam(value = "bookId") Long bookId
    ) {
        // 获取员工信息
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();
        long storeId = loginDto.getCurrentStoreId();

        Books books = booksService.getById(bookId);
        if (books == null) {
            return R.fail("书籍获取失败");
        }

        List<StoreBooks> storeBooksList = storeBooksService.getByStoreIdAndBooksId(storeId, bookId);
        if (CollectionUtils.isNotEmpty(storeBooksList)) return R.fail("门店不能重复导入这本书");

        StoreBooks storeBooks = BooksUtil.newStoreBooks(books, storeId, DateUtil.getNowDatetime(), books.getSource());
        storeBooksService.saveOrUpdate(storeBooks);

        BookSimpleDto bookSimpleDto = BooksUtil.newBookSimpleDto(storeBooks.getBooksId(), storeBooks.getId(), 1,
                1, storeBooks.getIsbn(), storeBooks.getBookNum(), storeBooks.getBookName(), storeBooks.getAuthor(),
                storeBooks.getBookLabelId(), storeBooks.getBookLabelName(), books.getBookType());
        return R.ok(bookSimpleDto);
    }

    @ApiOperation(value = "书籍库存数量加1")
    @PostMapping(value = "/addStockNum", produces = "application/json; charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public R<Object> addStockNum(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "门店书籍id") @RequestParam(value = "storeBookId") Long storeBookId,
            @ApiParam(value = "数量") @RequestParam(value = "num", required = false) Integer num
    ) {
        // 获取员工信息
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();
        long storeId = loginDto.getCurrentStoreId();

        StoreBooks storeBooks = storeBooksService.getByStoreIdAndId(storeId, storeBookId);
        if (storeBooks == null) {
            return R.fail("书籍获取失败");
        } else {
            storeBooksService.updateStockNum(storeBookId, num);
        }

        return R.ok();
    }

    @ApiOperation(value = "添加/编辑门店书籍信息")
    @PostMapping(value = "/editBooks", produces = "application/json; charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public R<Object> editBooks(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "分类id") @RequestParam(value = "bookLabelId", required = false) Long bookLabelId,
            @ApiParam(value = "分类") @RequestParam(value = "bookLabelName", required = false) String bookLabelName,
            @ApiParam(value = "总部书籍id") @RequestParam(value = "booksId", required = false) Long booksId,
            @ApiParam(value = "书号") @RequestParam(value = "isbn", required = false) String isbn,
            @ApiParam(value = "书名") @RequestParam(value = "bookName", required = false) String bookName,
            @ApiParam(value = "封面") @RequestParam(value = "img", required = false) String img,
            @ApiParam(value = "作者") @RequestParam(value = "author", required = false) String author,
            @ApiParam(value = "出版社") @RequestParam(value = "press", required = false) String press,
            @ApiParam(value = "出版日期") @RequestParam(value = "pubDate", required = false) String pubDate,
            @ApiParam(value = "简介") @RequestParam(value = "synopsis", required = false) String synopsis,
            @ApiParam(value = "介绍") @RequestParam(value = "introduction", required = false) String introduction,
            @ApiParam(value = "单价") @RequestParam(value = "unitPrice", required = false) Double unitPrice,
            @ApiParam(value = "库存") @RequestParam(value = "stockCount", required = false) Integer stockCount,
            @ApiParam(value = "可借数量") @RequestParam(value = "borrowCount", required = false) Integer borrowCount,
            @ApiParam(value = "状态：-1.下架 1.上架") @RequestParam(value = "status", required = false) Integer status
    ) throws Exception {
        // 获取员工信息
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();
        long storeId = loginDto.getCurrentStoreId();

        if (stockCount == null || stockCount < 0) {
            return R.fail("请填写库存数");
        }
        if (borrowCount == null || borrowCount < 0) {
            return R.fail("请填写可借数量");
        }

        StoreBooks storeBooks = null;
        if (id == null || id <= 0) {
            // 新增，门店手动添加，不同步书籍到系统书单
            storeBooks = new StoreBooks();
            // 设置书籍来源
            if (booksId != null && booksId > 0) {
                Books books = booksService.getById(booksId);
                if (books == null) return R.fail("系统书籍获取失败");

                storeBooks.setBookNum(books.getBookNum());
                storeBooks.setSource(books.getSource());
            } else {
                storeBooks.setSource(3);
            }
            storeBooks.setStoreId(storeId);
        } else {
            // 修改
            storeBooks = storeBooksService.getById(id);
            if (storeBooks == null)
                return R.fail("书籍获取失败");
        }
        storeBooks.setBookLabelId(bookLabelId);
        storeBooks.setBookLabelName(bookLabelName);
        storeBooks.setBooksId(booksId);
        storeBooks.setIsbn(isbn);
        storeBooks.setBookName(bookName);
        storeBooks.setImg(img);
        storeBooks.setAuthor(author);
        storeBooks.setPress(press);
        storeBooks.setPubDate(pubDate);
        storeBooks.setSynopsis(synopsis);
        storeBooks.setIntroduction(introduction);
        storeBooks.setUnitPrice(unitPrice);
        storeBooks.setStockCount(stockCount);
        storeBooks.setBorrowCount(borrowCount);
        storeBooks.setStatus(status);
        storeBooksService.saveOrUpdate(storeBooks);
        return R.ok();
    }

    @ApiOperation(value = "根据条件查询（借阅）")
    @PostMapping(value = "/getStoreBooks", produces = "application/json; charset=UTF-8")
    public R<Object> getStoreBooks(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "关键字") @RequestParam(value = "keyword", required = false) String keyword
    ) {
        // 获取员工信息
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();
        long storeId = loginDto.getCurrentStoreId();

        Page<StoreBooksDto> page = this.storeBooksService.pageList(pageUtil, storeId, keyword, null, null, null, null);

        PageDto<StoreBooksDto> data = new PageDto<>();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }

    @ApiOperation(value = "导出书籍")
    @PostMapping(value = "/exportList", produces = "application/vnd.ms-excel;charset=UTF-8")
    public R<Object> exportList(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "书刊id") @RequestParam(value = "journalId", required = false) Long journalId,
            @ApiParam(value = "分类id") @RequestParam(value = "bookLabelId", required = false) Long bookLabelId,
            @ApiParam(value = "状态：-1.下架 1.上架") @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "isbn查询状态：1.查isbn为空 2.查询isbn重复") @RequestParam(value = "isbnStatus", required = false) Integer isbnStatus,
            @ApiParam(value = "书号/isbn/书名/作者") @RequestParam(value = "keyword", required = false) String keyword
    ) throws Exception {
        // 获取员工信息
//        result = LoginAuthUtil.getRedisLoginDto(request);
//        LoginDataDto loginDto = (LoginDataDto) result.getData();
//        long storeId = loginDto.getCurrentStoreId();
//
//        List<StoreBooks> list = storeBooksService.getPageListResult(storeId, keyword, status, journalId, bookLabelId, isbnStatus);
//
//        if (CollectionUtils.isNotEmpty(list)) {
//            List<StoreBooksVO> booksVOS = new ArrayList<>();
//            for (StoreBooks books : list) {
//                StoreBooksVO data = new StoreBooksVO();
//                data.setBookName(NepUtil.nullToZero(books.getBookName()));
//                data.setIsbn(NepUtil.nullToZero(books.getIsbn()));
//                data.setPress(NepUtil.nullToZero(books.getPress()));
//                data.setAuthor(NepUtil.nullToZero(books.getAuthor()));
//                data.setPrice(NepUtil.nullToZero(books.getUnitPrice()));
//                data.setBookNum(NepUtil.nullToZero(books.getBookNum()));
//                data.setLabelName(NepUtil.nullToZero(books.getBookLabelName()));
//                data.setNum(NepUtil.nullToZero(books.getStockCount()));
//
//                booksVOS.add(data);
//            }
//
//            String fileName = URLEncoder.encode(System.currentTimeMillis() + "", "UTF-8");
//            excelHandler.exportExcel(response, booksVOS, StoreBooksVO.class, fileName, "书籍", false);
//        }
        return R.fail("该功能已暂停");
    }

    @ApiOperation(value = "判断是否能添加书籍v2")
    @PostMapping(value = "/whetherAddBook/v2", produces = "application/json; charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public R<Object> whetherAddBookV2(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "isbn/书号") @RequestParam(value = "isbn") String isbn
    ) {
        // 获取员工信息
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();
        long storeId = loginDto.getCurrentStoreId();

        if (isbn.length() < 8) return R.fail("isbn号或书号异常");

        // 判断门店书籍，相同的isbn和书号
        List<StoreBooks> storeBooksList = storeBooksService.getByStoreIdAndIsbnOrBookNum(storeId, isbn);
        if (CollectionUtils.isNotEmpty(storeBooksList)) {
            return R.fail(4001, "发现相同的门店书籍", storeBooksList);
        }

        // 判断系统书籍，相同的isbn和书号
        List<Books> booksList = booksService.getByIsbnOrBookNumNotInId(isbn, null);
        if (CollectionUtils.isNotEmpty(booksList)) {
            return R.fail(4002, "发现相同的系统书籍，是否导入", booksList);
        }

        return R.ok();
    }

    @ApiOperation(value = "判断是否能添加书籍v3")
    @PostMapping(value = "/whetherAddBook/v3", produces = "application/json; charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public R<Object> whetherAddBookV3(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "isbn") @RequestParam(value = "isbn") String isbn
    ) {
        // 获取员工信息
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();
        long storeId = loginDto.getCurrentStoreId();

        if (StringUtils.isBlank(isbn) || isbn.length() < 8) return R.fail("isbn号为空或不足8位");

        // 判断门店书籍，相同的isbn和书号
        List<StoreBooks> storeBooksList = storeBooksService.getByStoreIdAndIsbnOrBookNum(storeId, isbn);
        if (CollectionUtils.isNotEmpty(storeBooksList)) {
            return R.fail(4001, "发现相同的门店书籍", storeBooksList);
        }

        // 判断系统书籍，相同的isbn和书号
        List<Books> booksList = booksService.getByIsbnOrBookNumNotInId(isbn, null);
        if (CollectionUtils.isNotEmpty(booksList)) {
            return R.fail(4002, "发现相同的系统书籍，是否导入", booksList);
        } else {
            // 自营门店，开启阿里云查询书籍
            if (AccountUtil.isSelfStore(loginDto.getCurrentOperationType())) {
                R<AliyunBookDto.ResultDTO> bookR = AliyunBookUtil.getBook(isbn);
                if (R.isError(r)) return R.fail(bookR.getMsg());

                AliyunBookDto.ResultDTO data = bookR.getData();
                // 新增总部系统书籍
                String bookNum = null;
                // 获取当前最大书号
//                Books maxBooks = booksService.getMaxBookNum();
//                try {
//                    bookNum = BooksUtil.getBookNum(maxBooks);
//                } catch (Exception e) {
//                    return R.fail("阿里云查询书籍保存时，获取最新书号失败isbn：" + isbn);
//                }
                // 新增总部系统书籍
                Books book = BooksUtil.aliNewBooks(data, isbn, bookNum, null, null, null, null,
                        null, storeId, DateUtil.getNowDatetime());
                booksService.saveOrUpdate(book);
                if (book == null || book.getId() == null) return R.fail("阿里云查询书籍保存失败isbn：" + isbn);
                System.out.println("阿里云查询书籍保存成功isbn：" + isbn + "，书号：" + book.getBookNum());
                // 加入书籍集合
                booksList.add(book);

                return R.fail(4002, "发现相同的系统书籍，是否导入", booksList);
            }
        }

        return R.ok();
    }

    @ApiOperation(value = "查询门店书籍数量")
    @PostMapping(value = "/getStoreBookCnt", produces = "application/json; charset=UTF-8")
    public R<Object> getStoreBookCnt(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName
    ) {
        // 获取员工信息
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();
        long storeId = loginDto.getCurrentStoreId();

        int cnt = storeBooksService.getStoreBookCnt(storeId);

        return R.ok(cnt);
    }
}
